package com.example.test_aidl;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.util.Log;

public class MessengerActivity extends AppCompatActivity {
    public static final int Request_Code = 0;
    public static final int Repose_Code = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_messenger);
        Intent intent=new Intent(this,MessengerService.class);
        bindService(intent,serviceConnection,BIND_AUTO_CREATE);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(serviceConnection);
    }

    public Messenger messenger;


    @SuppressLint("HandlerLeak")
    private final Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            switch (msg.what) {
                case MessengerActivity.Request_Code:
                    Log.d("MessengerActivity", "handleMessage: Request_Code " + msg.getData().getInt("1"));
                    break;
                case MessengerActivity.Repose_Code:
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    int code = msg.getData().getInt("1");
                    code++;
                    Message message = Message.obtain();
                    message.what = MessengerActivity.Request_Code;
                    Bundle bundle = new Bundle();
                    bundle.putInt("1", code);
                    message.setData(bundle);
                    message.replyTo=messengerClient;
                    try {
                        messenger.send(message);
                    } catch (RemoteException e) {
                        e.printStackTrace();
                    }
                    Log.d("MessengerActivity", "handleMessage: Repose_Code " + msg.getData().getInt("1"));
                    break;
                default:break;
            }
        }
    };
    public Messenger messengerClient = new Messenger(handler);
    private ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            messenger = new Messenger(service);
            Message message = Message.obtain();
            message.what = MessengerActivity.Request_Code;
            Bundle bundle = new Bundle();
            bundle.putInt("1", 0);
            message.replyTo=messengerClient;
            message.setData(bundle);
            try {
                messenger.send(message);
            } catch (RemoteException e) {
                e.printStackTrace();
            }

        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };


}